/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/*!
  \file pthPage.h

  \author Cory Dixon
*/

#include "pthPage.h"
#include "vcConfig.h"

bool PTHPage::create_page(Client *client, GtkBuilder *builder)
{
	if( !client ) {
		cout << "PTHPage::create_page -- invalid client " << endl;
		return false;
	}
	if( page ) {
		cout << "PTHPage::create_page -- page has already been created" << endl;
		return true;
	}
	if( !builder ) {
		cout << "PTHPage::create_page -- invalid xml builder" << endl;
		return false;
	}

	page_client    = client;
	page           = GTK_WIDGET (gtk_builder_get_object(builder, "pth_page"));

	pressure_plot = GTK_WIDGET (gtk_builder_get_object(builder, "pressure_plot"));
	temp_plot     = GTK_WIDGET (gtk_builder_get_object(builder, "temp_plot"));
	humidity_plot = GTK_WIDGET (gtk_builder_get_object(builder, "humidity_plot"));

	// make sure we got all of our widgets
	if( !(page && pressure_plot && temp_plot && humidity_plot ) ) {
		cout << "PTHPage::create_page -- did not get all required widgets from builder" << endl;
		page = NULL;
		return false;
	}

	//--------------------------------------------------//
	// When we make the plots, we can use the fact tha they all
	// have the same name and type. they only differ by their pointer.
	
	gchar * setNames[MAX_NUM_SONDE];
	void  * setPtr[MAX_NUM_SONDE];
	int     setType[MAX_NUM_SONDE];

	GtkStripChart * graph;
	String chart_name;

	//-- pressure plot 
	update_pressure();
	for( int num_S=0; num_S < MAX_NUM_SONDE; num_S++ ) {
		setNames[num_S] =  g_strdup_printf("Sonde-%i", num_S);
		setPtr[num_S]   = &(pressure[num_S]);
		setType[num_S]  = FLOAT_T;
	}

	chart_name = page_client->name;
	chart_name += "/";
	chart_name += "pressure";
	graph = gtkstripchart_new (MAX_NUM_SONDE, (const gchar**)setNames, setPtr, setType, chart_name.c_str());
	graph->parent = pressure_plot;
	gtk_container_add (GTK_CONTAINER (pressure_plot),  graph->main_widget);
	gtk_container_set_border_width (GTK_CONTAINER (graph->main_widget), GNOME_PAD_SMALL);
	gtkstripchart_start (graph);

	//-- temperature plot 
	update_temp();
	for( int num_S=0; num_S < MAX_NUM_SONDE; num_S++ ) {
		setPtr[num_S] = &(temp[num_S]);
	}

	chart_name = page_client->name;
	chart_name += "/";
	chart_name += "temp";
	graph = gtkstripchart_new (MAX_NUM_SONDE, (const gchar**)setNames, setPtr, setType, chart_name.c_str());
	graph->parent = temp_plot;
	gtk_container_add (GTK_CONTAINER (temp_plot),  graph->main_widget);
	gtk_container_set_border_width (GTK_CONTAINER (graph->main_widget), GNOME_PAD_SMALL);
	gtkstripchart_start (graph);

	//-- humidity plot 
	update_humidity();
	
	for( int num_S=0; num_S < MAX_NUM_SONDE; num_S++ ) {
		setPtr[num_S] = &(humidity[num_S]);
	}

	chart_name = page_client->name;
	chart_name += "/";
	chart_name += "humid";
	graph = gtkstripchart_new (MAX_NUM_SONDE, (const gchar**)setNames, setPtr, setType, chart_name.c_str());
	graph->parent = humidity_plot;
	gtk_container_add (GTK_CONTAINER (humidity_plot),  graph->main_widget);
	gtk_container_set_border_width (GTK_CONTAINER (graph->main_widget), GNOME_PAD_SMALL);
	gtkstripchart_start (graph);

	for( int num_S=0; num_S < MAX_NUM_SONDE; num_S++ ) {
		g_free(setNames[num_S]);
	}
	return true;
}

//-- update humidity values in a smart manner
void PTHPage::update_humidity()
{
	for( int num_S=0; num_S < MAX_NUM_SONDE; num_S++ ) {
		if(  page_client->sonde[num_S].pth.rh1 != 9999 &&  page_client->sonde[num_S].pth.rh2 != 9999)
			humidity[num_S] = (page_client->sonde[num_S].pth.rh1 + page_client->sonde[num_S].pth.rh2) / 2.0;
		else if( page_client->sonde[num_S].pth.rh1 != 9999 )
			humidity[num_S] = page_client->sonde[num_S].pth.rh1;
		else if( page_client->sonde[num_S].pth.rh2 != 9999 )
			humidity[num_S] = page_client->sonde[num_S].pth.rh2;
		else
			humidity[num_S] = NAN;
	}
}
void PTHPage::update_pressure()
{
	for( int num_S=0; num_S < MAX_NUM_SONDE; num_S++ ) {
		if( page_client->sonde[num_S].pth.pressure != 9999 )
			pressure[num_S] = page_client->sonde[num_S].pth.pressure;
		else 
			pressure[num_S] = NAN;
	}
}
void PTHPage::update_temp()
{
	for( int num_S=0; num_S < MAX_NUM_SONDE; num_S++ ) {
		if( page_client->sonde[num_S].pth.temp != 9999 )
			temp[num_S] = page_client->sonde[num_S].pth.temp;
		else
			temp[num_S] = NAN;
	}
}

void PTHPage::update()
{
	//-- See if the pth_page has been initialized
	if( !page_client ) {
		cout << "PTHPage::update -- invalid page_client" << endl;
		return;
	}
	if( page == NULL ) {
		cout << "PTHPage::update -- page has not been created" << endl;
		return;
	}

	// don't do work if we don't need too
	if( !GTK_WIDGET_VISIBLE( page ) ) {
		return;
	}

	//-- update humidity values 
	update_humidity();
	update_pressure();
	update_temp();
		
	// redraw page
	gtk_widget_queue_draw(page);	
}

